package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.PersonGroupMapper;
import com.zhijian.medical.entity.po.PersonGroup;
import com.zhijian.medical.entity.vo.request.PersonGroupSearchVo;
import com.zhijian.medical.entity.vo.response.PersonGroupVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.PersonGroupService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-组织架构关系表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-24 15:27:14
 */
@Slf4j
@Service
public class PersonGroupServiceImpl implements PersonGroupService {

    @Autowired
    private PersonGroupMapper mapper;

    @Override
    public PersonGroup selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonGroupVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonGroup personGroup) {
        Date now = new Date();
        personGroup.setCreateTime(now);
        personGroup.setUpdateTime(now);
        return mapper.insert(personGroup) > 0 ? personGroup.getId() : 0L;
    }

    @Override
    public boolean update(PersonGroup personGroup) {
        Date now = new Date();
        personGroup.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personGroup) != 0;
    }

    @Override
    public Integer count(PersonGroupSearchVo personGroupSearchVo) {
        return mapper.count(personGroupSearchVo);
    }

    @Override
    public List<PersonGroupVo> selectList(PersonGroupSearchVo personGroupSearchVo) {
        return mapper.selectList(personGroupSearchVo);
    }

    @Override
    public Page<PersonGroupVo> selectPage(PersonGroupSearchVo personGroupSearchVo) {
        //先直接查总数
        int count = count(personGroupSearchVo);
        //分页
        Page<PersonGroupVo> page = new Page<>(true, count, personGroupSearchVo.getPageNum(), personGroupSearchVo.getPageSize());
        personGroupSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonGroupVo> responseVos = this.selectList(personGroupSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean connectGroup(Long personId, Long groupId) {
        Integer count = this.count(PersonGroupSearchVo.builder()
                .personId(personId)
                .groupId(groupId)
                .build());
        if (count > 0) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此人员已在此部门，请勿重复操作");
        }
        this.insert(PersonGroup.builder()
                .personId(personId)
                .groupId(groupId)
                .build());
        return true;
    }

    @Override
    public List<Long> selectGroupIdsByPersonId(Long personId) {
        return mapper.selectGroupIdsByPersonId(personId);
    }

}